package com.llm.travel_agent_assistant.controller;

import com.llm.travel_agent_assistant.dto.Result;
import com.llm.travel_agent_assistant.service.AsrService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping("/api/asr")
public class AsrController {

    @Resource
    private AsrService asrService;
    
    @Resource
    private HttpServletRequest request;

    /**
     * 实时语音识别接口
     * @param audioData 音频数据
     * @return 识别结果
     */
    @PostMapping("/realtime")
    public Result realtimeSpeechRecognition(@RequestBody byte[] audioData) {
        try {
            return asrService.realTimeSpeechRecognition(audioData);
        } catch (Exception e) {
            return Result.fail("语音识别失败: " + e.getMessage());
        }
    }

    /**
     * 流式实时语音识别接口
     * @param audioData 音频数据流
     * @return 识别结果
     */
    @PostMapping("/stream")
    public Result streamRealTimeRecognition(@RequestBody byte[] audioData) {
        try {
            // 检查session，验证用户是否登录
            HttpSession session = request.getSession(false);
            if (session == null) {
                return Result.fail("用户未登录");
            }
            
            String userId = (String) session.getAttribute("userId");
            if (userId == null) {
                return Result.fail("用户未登录");
            }

            return  asrService.streamRealTimeRecognition(audioData, userId);
        } catch (Exception e) {
            return Result.fail("流式语音识别失败: " + e.getMessage());
        }
    }


    /**
     * 流式实时语音识别接口
     * @param audioData 音频数据流
     * @return 识别结果
     */
    @PostMapping("/stream/budget_analyze")
    public Result streamRealTimeRecognitionForBudgetAnalyze(@RequestBody byte[] audioData) {
        try {
            // 检查session，验证用户是否登录
            HttpSession session = request.getSession(false);
            if (session == null) {
                return Result.fail("用户未登录");
            }

            String userId = (String) session.getAttribute("userId");
            if (userId == null) {
                return Result.fail("用户未登录");
            }

            return asrService.streamRealTimeRecognitionForBudgetAnalyze(audioData, userId);
        } catch (Exception e) {
            return Result.fail("流式语音识别失败: " + e.getMessage());
        }
    }

    /**
     * 文件语音识别接口
     * @param audioFile 音频文件
     * @return 识别结果
     */
    @PostMapping("/file")
    public Result fileSpeechRecognition(@RequestParam("file") MultipartFile audioFile) {
        try {
            // 将MultipartFile转换为临时文件
            File tempFile = File.createTempFile("audio", ".wav");
            audioFile.transferTo(tempFile);

            
            // 删除临时文件
            tempFile.delete();
            
            return asrService.syncSpeechRecognition(tempFile);
        } catch (Exception e) {
            return Result.fail("文件语音识别失败: " + e.getMessage());
        }
    }

    /**
     * 麦克风实时识别接口
     * @return 识别结果
     */
    @PostMapping("/microphone")
    public Result microphoneRecognition() {
        try {
            return  asrService.realTimeRecognitionFromMicrophone();
        } catch (Exception e) {
            return Result.fail("麦克风识别失败: " + e.getMessage());
        }
    }
}